/*
 * SRecord.java
 *
 * Created on March 20, 2007, 7:29 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package moto9s12sim;

/**
 *
 * @author Fergus Earnshaw
 */


import java.lang.*;

public class SRecord
{

    private String strSRec;
    private String strSRecType;
    private byte bCount;
    private short sAddr;
    private byte [] bData;
    private byte bChecksum;
    
    /** Creates a new instance of SRecord */
    public SRecord()
    {
        
    }
    
    public SRecord(String strTmpSRec)
    {
        strSRec = strTmpSRec;
        setSRecType();
        setSRecCount();
        setSRecAddr();
        setSRecData();
        setSRecChkSum();
        
    }   
    
    public void processSRecord (M9S12 CPU)
    {
             
        if (this.strSRecType.compareTo("S0") == 0)
        {
                this.ProcessS0(CPU);
        }
        
        else if(this.strSRecType.compareTo("S1") == 0)
        {
                this.ProcessS1(CPU);
        }        
        
        else if(this.strSRecType.compareTo("S2") == 0)
        {
                this.ProcessS2(CPU);
        }       
        
        else if(this.strSRecType.compareTo("S3") == 0)
        {
                this.ProcessS3(CPU);
        }        
        
        else if(this.strSRecType.compareTo("S5") == 0)
        {
                this.ProcessS5(CPU);
        }        
        
        else if(this.strSRecType.compareTo("S7") == 0)
        {
                this.ProcessS7(CPU);
        }
        
        else if(this.strSRecType.compareTo("S8") == 0)
        {
                this.ProcessS8(CPU);
        }        
        
        else if(this.strSRecType.compareTo("S9") == 0)
        {
                this.ProcessS9(CPU);
        }
        
        printSRecord(CPU);
        
    }
    
    private byte calcCheckSum()
    {
        return this.bChecksum;   
    }

    private void setSRecCount()
    {
        this.bCount = (byte)Integer.parseInt(this.strSRec.substring(2,4),16);        
    }
    
    private void setSRecAddr()
    {
        
        if ((this.strSRecType.compareTo("S0") == 0)||(this.strSRecType.compareTo("S1") == 0)||(this.strSRecType.compareTo("S5") == 0)||(this.strSRecType.compareTo("S9") == 0))
        {
            this.sAddr = (short)Integer.parseInt(this.strSRec.substring(4, 8), 16);
        }
        else if ((this.strSRecType.compareTo("S2") == 0)||(this.strSRecType.compareTo("S8") == 0))
        {
            this.sAddr = (short)Integer.parseInt(this.strSRec.substring(4, 10), 16);
        }
        
        else if ((this.strSRecType.compareTo("S3") == 0)||(this.strSRecType.compareTo("S7") == 0))
        {
            this.sAddr = (short)Integer.parseInt(this.strSRec.substring(4, 12), 16);            
        }        

    }
    
    private void setSRecType()
    {        
        this.strSRecType = this.strSRec.substring(0, 2);
    }
    
    private String GetSRecCount(String SRec)
    {
        return SRec.substring(2, 4);        
    }
    
    private void setSRecData()
    {   
        String strSRecData;
        
        strSRecData = "";
        
        if ((this.strSRecType.compareTo("S0") == 0)||(this.strSRecType.compareTo("S1") == 0)||(this.strSRecType.compareTo("S2") == 0)||(this.strSRecType.compareTo("S3") == 0))
        {
            if ((this.strSRecType.compareTo("S0") == 0)||(this.strSRecType.compareTo("S1") == 0))
            {
                strSRecData = this.strSRec.substring(8, this.strSRec.length()-2);
            }
            else if (this.strSRecType.compareTo("S2") == 0)
            {
                strSRecData = this.strSRec.substring(10, this.strSRec.length()-2);
            }
        
            else if (this.strSRecType.compareTo("S3") == 0)
            {
                strSRecData = this.strSRec.substring(12, this.strSRec.length()-2);
            }
           
            SetDataBytes(strSRecData);
        }       
        
    }
    
    private void setSRecChkSum()
    {
        this.bChecksum = (byte)Integer.parseInt(this.strSRec.substring(this.strSRec.length()-2, this.strSRec.length()),16);
        
    }
    
    private void SetDataBytes(String strBytes)
    {
        
        this.bData = new byte[strBytes.length()/2];
        
        for (int i = 0; i < strBytes.length()/2; i++)
        {
           this.bData[i] = (byte)Integer.parseInt(strBytes.substring(i*2,i*2+2),16);
        }
        
    }
    
    private void ProcessS0(M9S12 CPU)
    {
        
        //this contains header info currently know data is extracted.
        
    }
    private void ProcessS1(M9S12 CPU)
    {
        if (bData != null)
        {
            for (int i = this.sAddr; i < bData.length; i++)
            {
                CPU.bMemory[i] = this.bData[i];
            }
        }
    }
    
    private void ProcessS2(M9S12 CPU)
    {
        if (bData != null)
        {
            for (int i = this.sAddr; i < bData.length; i++)
            {
                CPU.bMemory[i] = this.bData[i];
            }
        }
    }
    
    private void ProcessS3(M9S12 CPU)
    {
        if (bData != null)
        {
            for (int i = this.sAddr; i < bData.length; i++)
            {
                CPU.bMemory[i] = this.bData[i];
            }
        }
    
    }
    
    private void ProcessS5(M9S12 CPU)
    {
        //This address field has count infor mation currently know processing is done with it this is a stub for that ocation
        
    }
 
    private void ProcessS7(M9S12 CPU)
    {
        
       CPU.PC.setRegister(this.sAddr);
       
    }
    
    private void ProcessS8(M9S12 CPU)
    {        
       CPU.PC.setRegister(this.sAddr);         
    }
    
    private void ProcessS9(M9S12 CPU)
    {
        CPU.PC.setRegister(this.sAddr);        
    }
    
    private void printSRecord(M9S12 CPU)
    {
          

        
        CPU.writelnToConsole("SRecord:" + this.strSRec);
        CPU.writelnToConsole("Record Type 0x:" + this.strSRecType);
        CPU.writelnToConsole("Count: 0x" + Integer.toHexString((this.bCount >= 0) ? this.bCount : 256 + this.bCount).toUpperCase());
        CPU.writelnToConsole("Address 0x:" + Integer.toHexString((this.sAddr >= 0) ? this.sAddr : 256 + this.sAddr).toUpperCase());
        CPU.writeToConsole("Data 0x:");
        if (bData != null)
        {
            for (int i = 0; i < bData.length; i++)
            {                
                CPU.writeToConsole(((String)Integer.toHexString((this.bData[i] >= 0) ? this.bData[i] : 256 + this.bData[i])).toUpperCase() + " ");
            }
        }        
        CPU.writelnToConsole("");
        CPU.writelnToConsole("Checksum in File 0x:" + Integer.toHexString((this.bChecksum >= 0) ? this.bChecksum : 256 + this.bChecksum).toUpperCase());
        CPU.writelnToConsole("Checksum Calculated 0x:" + Integer.toHexString((this.calcCheckSum() >= 0) ? this.calcCheckSum() : 256 + this.calcCheckSum()).toUpperCase());
        CPU.writelnToConsole("------------------------------------------------------------------------------------------------------------------------------------------");
      
            
    }
}
